package cn.jtfadmin.activiti.ueditor;

import cn.jtfadmin.activiti.ueditor.define.ActionMap;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.springframework.util.StreamUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ConfigManager {
    private final String rootPath;
    private final String originalPath;
    private final String contextPath;
    private  JsonNode jsonConfig = null;
    private static final String SCRAWL_FILE_NAME = "scrawl";
    private static final String REMOTE_FILE_NAME = "remote";

    private ConfigManager(String rootPath, String contextPath, String uri) throws FileNotFoundException, IOException {
        rootPath = rootPath.replace("\\", "/");
        this.rootPath = rootPath;
        this.contextPath = contextPath;
        if (contextPath.length() > 0) {
            this.originalPath = this.rootPath + uri.substring(contextPath.length());
        } else {
            this.originalPath = this.rootPath + uri;
        }

        this.initEnv();
    }

    public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {
        try {
            return new ConfigManager(rootPath, contextPath, uri);
        } catch (Exception var4) {
            return null;
        }
    }

    public boolean valid() {
        return this.jsonConfig != null;
    }

    public JsonNode getAllConfig() {
        return this.jsonConfig;
    }

    public Map<String, Object> getConfig(int type) {
        Map<String, Object> conf = new HashMap();
        String savePath = null;
        switch(type) {
            case ActionMap
                    .UPLOAD_IMAGE:
                conf.put("isBase64", "false");
                conf.put("maxSize", this.jsonConfig.get("imageMaxSize").asLong());
                conf.put("allowFiles", this.getArray("imageAllowFiles"));
                conf.put("fieldName", this.jsonConfig.get("imageFieldName").asText(""));
                savePath = this.jsonConfig.get("imagePathFormat").asText();
                break;
            case ActionMap.UPLOAD_SCRAWL:
                conf.put("filename", "scrawl");
                conf.put("maxSize", this.jsonConfig.get("scrawlMaxSize").asLong());
                conf.put("fieldName", this.jsonConfig.get("scrawlFieldName").asText());
                conf.put("isBase64", "true");
                savePath = this.jsonConfig.get("scrawlPathFormat").asText();
                break;
            case ActionMap.UPLOAD_VIDEO:
                conf.put("maxSize", this.jsonConfig.get("videoMaxSize").asLong());
                conf.put("allowFiles", this.getArray("videoAllowFiles"));
                conf.put("fieldName", this.jsonConfig.get("videoFieldName").asText());
                savePath = this.jsonConfig.get("videoPathFormat").asText();
                break;
            case ActionMap.UPLOAD_FILE:
                conf.put("isBase64", "false");
                conf.put("maxSize", this.jsonConfig.get("fileMaxSize").asLong());
                conf.put("allowFiles", this.getArray("fileAllowFiles"));
                conf.put("fieldName", this.jsonConfig.get("fileFieldName").asText());
                savePath = this.jsonConfig.get("filePathFormat").asText();
                break;
            case ActionMap.CATCH_IMAGE:
                conf.put("filename", "remote");
                conf.put("filter", this.getArray("catcherLocalDomain"));
                conf.put("maxSize", this.jsonConfig.get("catcherMaxSize").asLong());
                conf.put("allowFiles", this.getArray("catcherAllowFiles"));
                conf.put("fieldName", this.jsonConfig.get("catcherFieldName").asText() + "[]");
                savePath = this.jsonConfig.get("catcherPathFormat").asText();
                break;
            case ActionMap.LIST_FILE:
                conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
                conf.put("dir", this.jsonConfig.get("fileManagerListPath").asText());
                conf.put("count", this.jsonConfig.get("fileManagerListSize").asInt());
                break;
            case ActionMap.LIST_IMAGE:
                conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
                conf.put("dir", this.jsonConfig.get("imageManagerListPath").asText());
                conf.put("count", this.jsonConfig.get("imageManagerListSize").asInt());
        }

        conf.put("savePath", savePath);
        conf.put("rootPath", this.rootPath);
        return conf;
    }

    private void initEnv() throws FileNotFoundException, IOException {

        try ( InputStream resourceAsStream = getClass().getResourceAsStream("config.json");){
            String str = StreamUtils.copyToString(resourceAsStream, StandardCharsets.UTF_8);
            str = this.filter(str);
            JsonNode jsonConfig = new ObjectMapper().readTree(str);
            this.jsonConfig = jsonConfig;
        } catch (Exception var4) {
            this.jsonConfig = null;
        }

    }


    private String[] getArray(String key) {
        ArrayNode jsonArray = (ArrayNode) this.jsonConfig.get(key);
        String[] result = new String[jsonArray.size()];
        int i = 0;

        for(int len = jsonArray.size(); i < len; ++i) {
            result[i] = jsonArray.get(i).asText("");
        }

        return result;
    }


    private String filter(String input) {
        return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
    }
}
